package com.xmut.mall.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.manager.dao.MemberDao;
import com.xmut.mall.manager.entity.MemberEntity;
import com.xmut.mall.manager.service.MemberService;
import com.xmut.mall.manager.service.MenuService;
import com.xmut.mall.manager.utils.RespBean;
import com.xmut.mall.manager.vo.LoginVo;
import com.xmut.mall.manager.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    /**
     * 管理员后台登录
     * @param vo
     * @return
     */
    @Override
    public RespBean managerLogin(LoginVo vo) {
        MemberEntity member = this.getOne(new QueryWrapper<MemberEntity>().eq("username", vo.getUsername()));
        if(member==null){
            return RespBean.error("用户不存在");
        }else {
            // 进行验证
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean result = passwordEncoder.matches(vo.getPassword(), member.getPassword());
            if(result){
                member.setPassword(null);
                return RespBean.success("匹配用户成功",member);
            }else {
                return RespBean.error("密码错误");
            }
        }
    }
}
